package br.com.locanet.view.impl;

import javax.faces.bean.ManagedBean;

import br.com.locanet.bean.impl.Cliente;
import br.com.locanet.exception.InvalidAttributeException;
import br.com.locanet.service.impl.ClienteService;
import br.com.locanet.view.AbstractManagedBean;

@ManagedBean(name="clienteBean")
public class ClientesBean extends
		AbstractManagedBean<Cliente, ClienteService> {
	private String confSenha;
	
	public String insert() throws Exception {
		getEntity().setStatus(1);

		getEntityService().doValidateSenha(getEntity(),getConfSenha());
		
		getEntityService().insert(getEntity());
		return INSERT;
	}
	@Override
	public String remove() throws Exception {
		doExclusaoLogica(getEntity());
		select();
		return REMOVE;
	}
	private void doExclusaoLogica(Cliente clie) throws Exception {
		clie = getEntityService().selectById(clie);
		clie.setStatus(clie.getStatus() == 1 ? 0 : 1);
		getEntityService().update(clie);
	}

	public void select() throws Exception {
		getEntityService().select(getEntity());
	}
	
	public void get_update() throws Exception {
        if (getEntity().getCpf()==null) throw new InvalidAttributeException("cpfNaoInformado");
		
		Cliente cliente = getEntityService().selectById(getEntity());
		
		getRequest().setAttribute("cliente", cliente);
	}

	public String getConfSenha() {
		return confSenha;
	}

	public void setConfSenha(String confSenha) {
		this.confSenha = confSenha;
	}
}
